package com.demomap;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.guyj.CommonAdapter;
import com.guyj.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

public class CardListActivity extends AppCompatActivity {

    @Bind(R.id.recycler)
    RecyclerView recycler;
    LinearLayoutManager llm;
    CommonAdapter<CardBean> adapter;
    ArrayList<CardBean> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_list);
        ButterKnife.bind(this);
        llm = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        datas = SPUtil.getInstance().loadCardBean();
        recycler.setLayoutManager(llm);
        recycler.setAdapter(adapter = new CommonAdapter<CardBean>(this, R.layout.item_card, datas) {
            @Override
            protected void convert(ViewHolder viewHolder, CardBean cardBean, final int i) {
                viewHolder.setText(R.id.tv_content, cardBean.getContent());
                viewHolder.setText(R.id.tv_time, cardBean.getTime());
                ImageLoadManager.into(cardBean.getUrl(), (ImageView) viewHolder.getView(R.id.iv));
                viewHolder.setOnClickListener(R.id.root, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new SweetAlertDialog(CardListActivity.this, SweetAlertDialog.WARNING_TYPE)
                                .setTitleText("Are you sure?")
                                .setContentText("delete this item?")
                                .setCancelText("No!")
                                .setConfirmText("Yes,delete it!")
                                .showCancelButton(true)
                                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sDialog) {
                                        sDialog.setTitleText("Cancelled!")
                                                .setContentText("item is safe :)")
                                                .setConfirmText("OK")
                                                .showCancelButton(false)
                                                .setCancelClickListener(null)
                                                .setConfirmClickListener(null)
                                                .changeAlertType(SweetAlertDialog.ERROR_TYPE);
                                    }
                                })
                                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sDialog) {
                                        datas.remove(i);
                                        SPUtil.getInstance().saveCardBean(datas);
                                        adapter.setDatas(datas);
                                        sDialog.setTitleText("Deleted!")
                                                .setContentText("Item has been deleted!")
                                                .setConfirmText("OK")
                                                .showCancelButton(false)
                                                .setCancelClickListener(null)
                                                .setConfirmClickListener(null)
                                                .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                                    }
                                })
                                .show();
                    }
                });
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        datas = SPUtil.getInstance().loadCardBean();
        adapter.setDatas(datas);
    }

    @OnClick(R.id.iv_add)
    public void onClick() {
        startActivity(new Intent(this, InputActivity.class));
    }
}
